package com.isnoam.calendar.view

import android.content.Context
import android.view.View
import android.view.View.OnClickListener
import android.widget.ImageView
import android.widget.TextView
import com.isnoam.calendar.R
import com.isnoam.calendar.base.BaseHeaderView
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Locale

/**
 * 默认头部
 */
open class DefaultHeaderView(
    context: Context
) : BaseHeaderView(context), OnClickListener {

    private lateinit var titleLabel: TextView
    private lateinit var prevMonth: ImageView
    private lateinit var nextMonth: ImageView

    override fun getLayoutId(): Int {
        return R.layout.default_header_view
    }

    /**
     * 实始化
     */
    override fun init() {
        titleLabel = findViewById(R.id.title_label)
        prevMonth = findViewById(R.id.prev_month)
        nextMonth = findViewById(R.id.next_month)

        prevMonth.setOnClickListener(this)
        nextMonth.setOnClickListener(this)
    }

    /**
     * 更新标题
     */
    override fun updateTitle(year: Int, month: Int) {
//        var yearMonth = "${year}年${month}月"
//        titleLabel.text = yearMonth
        updateETitle(year,month)
    }

    private fun updateETitle(year: Int, month: Int) {
        val calendar = Calendar.getInstance()
        calendar.set(Calendar.YEAR, year)
        calendar.set(Calendar.MONTH, month - 1)
        val dateFormat = SimpleDateFormat("MMMM yyyy", Locale.ENGLISH)
        val yearMonth = dateFormat.format(calendar.time)
        titleLabel.text = yearMonth.toUpperCase(Locale.ENGLISH)
    }

    /**
     * 处理上、下按钮
     */
    override fun handlePrevNext(hasPrev: Boolean, hasNext: Boolean) {
        if (hasPrev) {
            prevMonth.visibility = View.VISIBLE
        } else {
            prevMonth.visibility = View.INVISIBLE
        }
        if (hasNext) {
            nextMonth.visibility = View.VISIBLE
        } else {
            nextMonth.visibility = View.INVISIBLE
        }
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.prev_month -> {
                listener?.prevMonth()
            }

            R.id.next_month -> {
                listener?.nextMonth()
            }
        }
    }
}